﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using LinqKit;
using Webcar.Entitys;
using Webcar.Utils;
namespace Webcar.BLL
{
    public class CurrentUser
    {
        public static string LoginUserID
        {
            get
            {
                return HttpContext.Current.User.Identity.Name;
            }
        }
        public static bool CheckPermission(string permission_id)
        {
            if (LoginUserID.IsNull()) return false;
            if (IsSuperUser) return true;
            var item = WebcarDataProvider.DB.v_user_purview.Where(a => a.userid == LoginUserID && a.purview_id == permission_id&&a.purview_type=="grant");
            return item.Count() > 0;
        }
        public static bool CheckPermission(Permission permission)
        {
            if (IsSuperUser) return true;
            return CheckPermission(permission.PermissionId);
        }
        public static void EnsurePermission(Permission permission)
        {
            if (CheckPermission(permission) == false)
                throw new Exception("你没有访问此功能的权限。");
        }
        public static void EnsureAdmin()
        {
            if (!IsSuperUser) throw new Exception("你没有访问此功能的权限。");
        }

        public static bool IsSuperUser
        {
            get
            {
                var user = GetCurrentUser();
                if (user != null)
                    return user.is_super == "T";
                return false;
            }
        }

        public static IQueryable<v_sys_dept_all> UserGrantedDept
        {
            get
            {
                IQueryable<v_sys_dept_all> result = null;
                if (CurrentUser.IsSuperUser)
                {
                    result = (from p in Webcar.Entitys.WebcarDataProvider.DB.v_sys_dept_all
                              where p.use_flag=="T"
                              select p);
                }
                else
                {
                    result = (from p in Webcar.Entitys.WebcarDataProvider.DB.v_sys_dept_all
                              join p2 in WebcarDataProvider.DB.BS_MAN_DEPT
                              on p.deptcode equals p2.DEPT_CODE
                              where p.use_flag == "T" && p2.MAN_CODE == CurrentUser.LoginUserID
                              select p);
                }
                return result;
            }
        }

        //public static IQueryable<DEPT_CHILD> UserGrantedChildDept
        //{
        //    get
        //    {
        //        IQueryable<DEPT_CHILD> result = null;
        //        if (CurrentUser.IsSuperUser)
        //        {
        //            result = (from p in Webcar.Entitys.WebcarDataProvider.DB.DEPT_CHILD
        //                      where p.is_use == "T"
        //                      select p);
        //        }
        //        else
        //        {
        //            result = (from p in WebcarDataProvider.DB.DEPT_CHILD
        //                      join p2 in WebcarDataProvider.DB.bs_man_childdept
        //                      on p.CHILD_DEPT equals p2.child_dept
        //                      where p.is_use == "T" && p2.userid == CurrentUser.LoginUserID
        //                      select p);
        //        }
        //        return result;
        //    }
        //}
        //public static IQueryable<T> FilterChildDept<T, TProperty>(IQueryable<T> source, Expression<Func<T, TProperty>> field)
        //{
        //    PredicateBuilder.True<T>(a)
        //    source
        //    string name = ExpressionHelper.GetExpressionText(field);
        //    //var list = source.Where("CAR_NO='000001143M' And 1=1");
        //    var all_childs = UserGrantedChildDept.Select(a => a.CHILD_DEPT);
        //    return null;
        //}
        static v_all_users GetCurrentUser()
        {
            return Webcar.Entitys.WebcarDataProvider.DB.v_all_users.FirstOrDefault(a => a.account == LoginUserID || a.cn == LoginUserID);
        }
        public static string GetLoginUserName()
        {
            var user = GetCurrentUser();
            if (user != null)
            {
                return user.name;
            }
            else
            {
                return "未知用户";
            }
        }
        public static string GetLoginDeptName()
        {
            var user = GetCurrentUser();
            if (user != null)
            {
                return user.corpname+"--"+user.deptname;
            }
            else
            {
                return "未知部门";
            }
        }

        public static string Login(string userid, string userpass)
        {
            var user = GetCurrentUser();
            if (user == null)
            {
                throw new Exception("该用户不存在。");
            }
            if (user.loginenable!="1")
                throw new Exception("该用户已经被禁用。请联系系统管理员。");
            if (user.password != GetEnyptPass(userpass) && user.oldpass != GetEnyptPass(userpass))
            {
                throw new Exception("该用户名和密码不匹配。");
            }

            return user.account;
        }

        public static string GetEnyptPass(string pass)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(pass);
            bs = md5.ComputeHash(bs);
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("X2"));
            }
            //string password = s.ToString();
            return s.ToString();
        }
    }
}